package com.example.demo.controller;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.demo.common.Result;
import com.example.demo.entity.Params;
import com.example.demo.entity.Type;
import com.example.demo.exception.CustomException;
import com.example.demo.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("/type")
public class TypeController {
    @Resource
    private TypeService typeService;
    @PostMapping
    public Result save(@RequestBody Type type) {
        if (type.getId() == null) {
            typeService.add(type);
            return Result.success();
        } else {
            typeService.update(type);
            return Result.success();
        }
    }
    @GetMapping
    public Result findAll()
    {
        return Result.success(typeService.findAll());

    }
    @GetMapping("/search")
    public Result findBySearch(Params params) {
        PageInfo<Type> homeworklist = typeService.findBySearch(params);
        return Result.success(homeworklist);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        typeService.delete(id);
        return Result.success();
    }
    @PutMapping("/delBatch")
    public Result deleteBatch(@RequestBody List<Type> list)
    {
        for (Type type : list) {
            typeService.delete(type.getId());
        }
       return Result.success();
    }
    @GetMapping("/export")
    public Result export(HttpServletResponse response) throws IOException {

        //1.查询数据库数据
        List<Type> all = typeService.findAll();
        //2.判断
        if (CollectionUtil.isEmpty(all)) {
            throw new CustomException("未找到数据");
        }
        //3.定义list map
        List<Map<String,Object>> list =new ArrayList<>(all.size());
        //4.遍历数据库中的数据
        for (Type type : all) {
            Map<String,Object> row=new HashMap<>();
            row.put("分类名称",type.getName());
            row.put("分类描述",type.getDescription());
            list.add(row);
        }
        //5.创建一个ExcelWriter
        ExcelWriter wr = ExcelUtil.getWriter(true);  // 假设 ExcelUtil 中有这样的方法来获取 ExcelWriter 并设置是否为 xlsx 格式
        wr.write(list, true);  // 假设 write 方法可以接受列表数据和是否将键作为表头的参数

        // 5. 把这个 excel 下载下来
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=type.xlsx");
        try {
            ServletOutputStream out = response.getOutputStream();
            wr.flush(out, true);  // 假设 flush 方法可以接受输出流和是否关闭的参数
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success();
    }
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        List<Type> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(Type.class);
        if (!CollectionUtil.isEmpty(infoList)) {
            for (Type type : infoList) {
                try {
                    typeService.add(type);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.success();
    }
}
